const { spawn } = require('child_process');

function restartIndex() {
  const indexProcess = spawn('node', ['server.js']);
  indexProcess.stdout.on('data', (data) => {
    console.log(data.toString('utf8'));
  });
  indexProcess.stderr.on('data', (data) => {
    console.error(data.toString('utf8'));
  });
  indexProcess.on('close', (code) => {
    console.log(`[process] index exited with code ${code}`);
    restartIndex();
  });
  indexProcess.on('message', (msg) => {
    if (msg === 'restart') {
      indexProcess.kill();
      restartIndex();
    }
  });
}
restartIndex();